package com.honghe.system.service.impl;

import java.util.List;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.honghe.system.mapper.CustomerContactRecordMapper;
import com.honghe.system.domain.CustomerContactRecord;
import com.honghe.system.service.ICustomerContactRecordService;
import com.honghe.system.domain.BankCustomerCases;
import com.honghe.common.utils.SecurityUtils;

/**
 * 客户联系记录Service业务层处理
 * 
 * @author honghe
 */
@Service
public class CustomerContactRecordServiceImpl implements ICustomerContactRecordService 
{
    private static final Logger log = LoggerFactory.getLogger(CustomerContactRecordServiceImpl.class);

    @Autowired
    private CustomerContactRecordMapper customerContactRecordMapper;

    /**
     * 查询客户联系记录
     * 
     * @param recordId 客户联系记录主键
     * @return 客户联系记录
     */
    @Override
    public CustomerContactRecord selectCustomerContactRecordByRecordId(Long recordId)
    {
        return customerContactRecordMapper.selectCustomerContactRecordByRecordId(recordId);
    }

    /**
     * 查询客户联系记录列表
     * 
     * @param customerContactRecord 客户联系记录
     * @return 客户联系记录
     */
    @Override
    public List<CustomerContactRecord> selectCustomerContactRecordList(CustomerContactRecord customerContactRecord)
    {
        return customerContactRecordMapper.selectCustomerContactRecordList(customerContactRecord);
    }

    /**
     * 根据客户ID查询联系记录列表
     * 
     * @param caseId 客户ID
     * @return 客户联系记录集合
     */
    @Override
    public List<CustomerContactRecord> selectCustomerContactRecordByCaseId(String caseId)
    {
        return customerContactRecordMapper.selectCustomerContactRecordByCaseId(caseId);
    }

    /**
     * 新增客户联系记录
     * 
     * @param customerContactRecord 客户联系记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCustomerContactRecord(CustomerContactRecord customerContactRecord)
    {
        log.info("开始新增联系记录");
        log.info("接收到的联系记录数据: {}", customerContactRecord);
        
        try {
            // 数据校验
            if (customerContactRecord.getCaseId() == null || customerContactRecord.getCaseId().trim().isEmpty()) {
                log.error("客户ID不能为空");
                throw new IllegalArgumentException("客户ID不能为空");
            }
            
            if (customerContactRecord.getContactType() == null || customerContactRecord.getContactType().trim().isEmpty()) {
                log.error("联系方式不能为空");
                throw new IllegalArgumentException("联系方式不能为空");
            }
            
            if (customerContactRecord.getContactPerson() == null || customerContactRecord.getContactPerson().trim().isEmpty()) {
                log.error("联系人不能为空");
                throw new IllegalArgumentException("联系人不能为空");
            }
            
            if (customerContactRecord.getContactResult() == null || customerContactRecord.getContactResult().trim().isEmpty()) {
                log.error("联系结果不能为空");
                throw new IllegalArgumentException("联系结果不能为空");
            }
            
            // 如果联系时间为空，设置为当前时间
            if (customerContactRecord.getContactTime() == null) {
                log.info("联系时间为空，设置为当前时间");
                customerContactRecord.setContactTime(new Date());
            }
            
            // 设置创建者为当前登录用户
            String username = SecurityUtils.getUsername();
            customerContactRecord.setCreateBy(username);
            
            // 设置创建时间
            customerContactRecord.setCreateTime(new Date());
            
            // 执行插入操作
            log.info("执行数据库插入操作");
            int rows = customerContactRecordMapper.insertCustomerContactRecord(customerContactRecord);
            log.info("数据库插入结果: {}", rows);
            
            return rows;
        } catch (Exception e) {
            log.error("新增联系记录失败", e);
            throw e;
        }
    }

    /**
     * 修改客户联系记录
     * 
     * @param customerContactRecord 客户联系记录
     * @return 结果
     */
    @Override
    public int updateCustomerContactRecord(CustomerContactRecord customerContactRecord)
    {
        customerContactRecord.setUpdateTime(new Date());
        return customerContactRecordMapper.updateCustomerContactRecord(customerContactRecord);
    }

    /**
     * 批量删除客户联系记录
     * 
     * @param recordIds 需要删除的客户联系记录主键
     * @return 结果
     */
    @Override
    public int deleteCustomerContactRecordByRecordIds(Long[] recordIds)
    {
        return customerContactRecordMapper.deleteCustomerContactRecordByRecordIds(recordIds);
    }

    /**
     * 删除客户联系记录信息
     * 
     * @param recordId 客户联系记录主键
     * @return 结果
     */
    @Override
    public int deleteCustomerContactRecordByRecordId(Long recordId)
    {
        return customerContactRecordMapper.deleteCustomerContactRecordByRecordId(recordId);
    }

    /**
     * 根据客户ID查询客户信息
     * 
     * @param caseId 客户ID
     * @return 客户信息
     */
    @Override
    public BankCustomerCases selectCustomerByCaseId(String caseId)
    {
        return customerContactRecordMapper.selectCustomerByCaseId(caseId);
    }
    
    /**
     * 查询用户有权限查看的客户列表（负责人或经办人是当前用户）
     * 
     * @param username 用户名
     * @return 客户信息集合
     */
    @Override
    public List<BankCustomerCases> selectCustomersByUsername(String username)
    {
        return customerContactRecordMapper.selectCustomersByUsername(username);
    }
}